package com.example.administrator.x2picturetransliteration.Home.Presenter.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Base.HttpGo;
import com.example.administrator.x2picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x2picturetransliteration.Constants;
import com.example.administrator.x2picturetransliteration.Dao.Rx.UserRx;
import com.example.administrator.x2picturetransliteration.Dao.Table.User;
import com.example.administrator.x2picturetransliteration.Home.Activity.AppActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 注册
 * Created by Administrator on 2019/9/3.
 */

public class SignInPresenter extends BasePresenter{

    public SignInPresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 登录 */
    public void login(final Map<String,String> httpdata){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/login", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()==null)return;

                            Map<String,String> map = (Map<String,String>)httpDataBean.getData();

                            updateUserDao(map.get("token"),httpdata.get("mobile"),httpdata.get("password"));

                        }
                    }
                })
        );

    }

    /** 微信登录 */
    public void wxlogin(final Map<String,String> httpdata, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/wxlogin", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null){

                                mCallback.getData(httpDataBean.getData());
                            }
                        }
                    }
                })
        );
    }

    /** QQ登录 */
    public void qqlogin(final Map<String,String> httpdata, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/qqlogin", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null){

                                mCallback.getData(httpDataBean.getData());
                            }
                        }
                    }
                })
        );
    }

    /** 查询数据库有没有上次登录的信息,如果有直接登录 */
    public void userDao(){
        mCompositeSubscription.add(
                new UserRx().queryAll()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<User>>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(List<User> users) {
                                if(users.size()!=0){

                                    Constants.getConstants().setToken(users.get(0).getData());

                                    Intent intent = new Intent(context,AppActivity.class);
                                    context.startActivity(intent);
                                    ((Activity)context).finish();

                                } else {

                                }
                            }
                        })
        );

    }

    /** 登录成功之后将用户信息写入sqlint */
    public void updateUserDao(final String token, String userId, String Ped){

        User user = new User();
        user.setUserid(userId);
        user.setPassword(Ped);
        user.setData(token);

        Constants.getConstants().setToken(token);


        new UserRx().saveData(user, new UserRx.CallbackUserRx() {
            @Override
            public void Method() {

                context.startActivity(new Intent(context,AppActivity.class));
                ((Activity)context).finish();
            }
        });
    }

    /** 获取用户协议 */
    public void Agreement(String type,final Callback mCallback){

        Map<String,String> httpdata = new HashMap<>();

        httpdata.put("id",type);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("sys_protocol/get", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()==null)return;

                            Map<String,String> map = (Map<String,String>)httpDataBean.getData();

                            mCallback.getData(map.get("content"));
                        }
                    }
                })
        );

    }

}
